<!--  -->
<template>
  <div id="home">
    <!-- 顶部导航组件 -->
    <nav-bar class="home-nav-bar">
      <div slot="center">购物街</div>
    </nav-bar>

    <!-- 流行新款精选菜单切换组件 -->
    <tab-control
      :titles="titles"
      @tabClick="tabClick"
      ref="tabControlTop"
      class="tab-control-top"
      v-show="isShowTabControl"
    ></tab-control>

    <scroll
      class="home-scroll"
      ref="scroll"
      :probeType="3"
      :pullUpLoad="true"
      @pullingUp="loadMore"
      @scroll="getScrollPosition"
    >
      <!-- 轮播图组件 -->
      <home-swiper
        class="home-swiper"
        :banners="banners"
        @swiperImgLoad="swiperImgLoaded"
      ></home-swiper>

      <!-- 轮播图下面图片导航组件 -->
      <home-recommend :recommends="recommends"></home-recommend>

      <!-- 本周流行 -->
      <week-popular></week-popular>

      <!-- 流行新款精选菜单切换组件 -->
      <tab-control
        :titles="titles"
        @tabClick="tabClick"
        ref="tabControl"
      ></tab-control>

      <!-- 商品列表 -->
      <goods-list :goods="showGoods"></goods-list>
    </scroll>

    <back-top v-show="showBackTop" @click.native="backTopClick"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import TabControl from "components/content/tabControl/TabControl.vue";
import GoodsList from "components/content/goods/GoodsList.vue";
import BackTop from "components/content/backtop/BackTop.vue";

import request from "network/home";
import Mixin from "common/mixin";

import HomeSwiper from "./childComps/HomeSwiper.vue";
import HomeRecommend from "./childComps/HomeRecommend.vue";
import WeekPopular from "./childComps/WeekPopular.vue";

export default {
  name: "Home",
  data() {
    return {
      banners: [],
      recommends: [],
      titles: ["流行", "新款", "精选"],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentShow: "pop",
      tabControlOffsetTop: null,
      isShowTabControl: false,

      //记录离开此界面时的位置
      pageOffsetTop:0
    };
  },
  mixins: [Mixin.backTopMixin],
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommend,
    WeekPopular,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  updated() {
    this.$refs.scroll.refresh();
  },
  created() {
    //请求轮播图数据
    this.getMultiData();
    //请求首页商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  activated() {
    // 重新更新home页面里的元素高度
    this.$refs.scroll.refresh();

    // 离开home页面再次进入时，打开的是离开时的位置
    this.$refs.scroll.scrollTo(0, this.pageOffsetTop, 0);
  },

  deactivated() {
    this.pageOffsetTop = this.$refs.scroll.getPageScrollY();
  },

  computed: {
    showGoods() {
      return this.goods[this.currentShow].list;
    },
  },

  methods: {
    //请求首页轮播图数据
    getMultiData() {
      request.getMultiData().then((res) => {
        this.banners = res.data.data.banner.list;
        this.recommends = res.data.data.recommend.list;
      });
    },

    //请求首页商品数据
    getHomeGoods(type) {
      var page = this.goods[type].page + 1;
      request.getHomeGoods(type, page).then((res) => {
        //console.log(res.data.data.list);
        this.goods[type].list.push(...res.data.data.list);
        this.goods[type].page += 1;
      });
    },

    //标签栏切换请求对应类型的数据
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentShow = "pop";
          break;
        case 1:
          this.currentShow = "new";
          break;
        case 2:
          this.currentShow = "sell";
          break;
      }

      this.$refs.tabControl.currentIndex = index;
      this.$refs.tabControlTop.currentIndex = index;
    },

    loadMore() {
      //console.log("滚动到底部了");
      this.getHomeGoods(this.currentShow);
      this.$refs.scroll.finishPullUp();
    },
    swiperImgLoaded() {
      // 计算中间选项卡元素到顶部的高度，并保存
      //this.$refs.scroll.refresh();
      this.tabControlOffsetTop = this.$refs.tabControl.$el.offsetTop;
    },
  },
};
</script>
<style lang='scss' scoped>
#home {
  position: relative;
  height: 100vh;
}
.home-nav-bar {
  z-index: 2;
  background-color: var(--color-tint);
  color: #fff;
  font-size: 20px;
  font-family: "微软雅黑";
}

.home-scroll {
  position: absolute;
  left: 0;
  right: 0;
  top: 44px;
  bottom: 49px;
  overflow: hidden;
}

.tab-control-top {
  position: absolute;
  top: 43px;
  left: 0;
  z-index: 2;
  background-color: #fff;
}
</style>